<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;

/**
 * Class StaffPeriodFee.
 *
 * @package namespace App\Models;
 */
class StaffPeriodFee extends BaseModel implements Transformable
{
    use TransformableTrait;

    protected $appends = ['total_fee'];
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['staff_id', 'period_id', 'water_fee', 'hot_water_fee', 'electric_fee', 'manage_fee', 'deposit_fee', 'adjust_fee', 'dorm_days', 'adjust_user_id', 'adjust_reason'];
    public function staff()
    {
        return $this->belongsTo(Staff::class);
    }

    public function period()
    {
        return $this->belongsTo(Period::class);
    }

    public function staffPeriodFeeDetails()
    {
        return $this->hasMany(StaffPeriodFeeDetail::class);
    }

    public function adjust_user()
    {
        return $this->belongsTo(User::class, 'adjust_user_id');
    }

    public function getTotalFeeAttribute()
    {
        $total = 0;
        if ($this->electric_fee) $total += $this->electric_fee;
        if ($this->water_fee) $total += $this->water_fee;
        if ($this->hot_water_fee) $total += $this->hot_water_fee;
        if ($this->manage_fee) $total += $this->manage_fee;
        if ($this->deposit_fee) $total += $this->deposit_fee;
        if ($this->adjust_fee) $total += $this->adjust_fee;
        return round($total, 2);
    }
}
